package com.compass.doctor.ui.mine

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.compass.doctor.R
import com.compass.doctor.databinding.ActivityDoctorHomepageBinding
import com.compass.doctor.dialog.DoctorGoodAtDialog
import com.compass.doctor.base.BaseVBActivity
import com.compass.doctor.ui.home.view.ArticleAndVideoView
import com.compass.doctor.ui.mine.adapter.EvaluateAdapter
import com.compass.doctor.ui.mine.adapter.EvaluateTagAdapter
import com.compass.doctor.ui.mine.viewmodel.MineViewModel
import com.compass.doctor.ui.setting.ConsultSettingActivity
import com.compass.framework.ext.gone
import com.compass.framework.ext.onClick
import com.compass.framework.ext.visible
import com.compass.framework.glide.setUrlCircle
import com.compass.framework.manager.UserInfoManager
import com.compass.framework.model.PrescriptionStatus
import com.compass.framework.utils.StatusBarSettingHelper
import com.compass.framework.utils.getColorFromResource
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayoutManager
import com.google.android.flexbox.JustifyContent

/**
 * Description： 我的主页
 * Date:2023/12/13 15:45
 */
class DoctorHomepageActivity : BaseVBActivity<ActivityDoctorHomepageBinding>() {

    companion object {
        fun start(context: Context) {
            val intent = Intent(context, DoctorHomepageActivity::class.java)
            context.startActivity(intent)
        }
    }

    private val mineViewModel: MineViewModel by viewModels()
    private var tags: MutableList<PrescriptionStatus> = mutableListOf()
    private lateinit var evaluateTagAdapter: EvaluateTagAdapter
    private lateinit var articleAndVideoView: ArticleAndVideoView
    private lateinit var evaluateAdapter: EvaluateAdapter
    override fun initView(savedInstanceState: Bundle?) {
        StatusBarSettingHelper.setStatusBarTranslucent(this)
        StatusBarSettingHelper.statusBarLightMode(this@DoctorHomepageActivity, true)
        articleAndVideoView = ArticleAndVideoView(this)
        evaluateTagAdapter = EvaluateTagAdapter()
        mBinding.flyArticleVideo.addView(articleAndVideoView)

        evaluateAdapter = EvaluateAdapter()
        mBinding.rcyEvaluate.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = evaluateAdapter
        }
        mBinding.individualResume.onClick {
            DoctorGoodAtDialog.Builder(this@DoctorHomepageActivity).show()

        }
        mBinding.tvTariffSetting.onClick {
            ConsultSettingActivity.start(this@DoctorHomepageActivity)
        }
        mBinding.tvEvaluateMore.onClick {

            EvaluateActivity.start(this@DoctorHomepageActivity)
        }
        //如果是医生
        if (UserInfoManager.getUserType() == "1"){
            mBinding.llOnLine.visible()
        }else{
            mBinding.llOnLine.gone()
        }
        initUserInfo()
    }

    private fun initUserInfo() {
        val user = UserInfoManager.getUserInfo()
        mBinding.apply {
            user?.let {
                doctorName.text = it.name
                doctorAvatar.setUrlCircle(it.avatar)
                tvPositionName.text = it.positionName
                hospitalName.text = it.hospitalName
                tvDepartment.text = it.departmentName
                tvHospitalLevel.text = it.hospitalLevel
//                if (it.authentication == 1) certification.visible() else certification.gone()
                if (it.prescriptionAllow == 1) tvPrescriptionAuthority.visible() else tvPrescriptionAuthority.gone()
                tvSpecialtyName.text = it.meritIllness?:"暂未填写"
                tvTherapeuticRegimen.text = it.meritTherapeutic?:"暂未填写"

            }
            user?.statistics?.let {
                tvConsultNumber.text = it.consultationNum
                tvEvaluate.text = it.favorableRate
                tvReply.text = it.followersNum

            }
            user?.config?.let {
                if (it.txtStatus == 1) {
                    imgTextIsOpen.text = "￥${it.txtPrice}/次"
                    imgTextIsOpen.setTextColor(getColorFromResource(R.color.color_ff4e32))
                } else {
                    imgTextIsOpen.text = "暂未开通"
                    imgTextIsOpen.setTextColor(getColorFromResource(R.color.color_999999))
                }
                if (it.phoneStatus == 1) {
                    telephoneTreatmentIsOpen.text = "￥${it.phonePrice}/次"
                    telephoneTreatmentIsOpen.setTextColor(getColorFromResource(R.color.color_ff4e32))
                } else {
                    telephoneTreatmentIsOpen.text = "暂未开通"
                    telephoneTreatmentIsOpen.setTextColor(getColorFromResource(R.color.color_999999))
                }
                if (it.videoStatus == 1) {
                    videoReceptionIsOpen.text = "￥${it.videoPrice}/次"
                    videoReceptionIsOpen.setTextColor(getColorFromResource(R.color.color_ff4e32))
                } else {
                    videoReceptionIsOpen.text = "暂未开通"
                    videoReceptionIsOpen.setTextColor(getColorFromResource(R.color.color_999999))
                }
            }

        }

    }

    override fun initData() {
        mineViewModel.getHomeVideoAndArticle()//文章视频数据
        mineViewModel.homeVideoArticleLiveData.observe(this){
            it?.let {
                articleAndVideoView.initData(it.articleList,it.videoList)
            }
        }

//        mineViewModel.getEvaluationClassification()
//        mineViewModel.classLiveData.observe(this) {
//            it?.let { evaluation ->
//                mBinding.tvFavorableRate.text = "${evaluation.favorableRate}%"
//                mBinding.tvService.text = "服务人数${evaluation.consultationNum}"
//                mBinding.tvEvaluatorsNumber.text = "共${evaluation.evaluatorsNumber}人评价"
//                tags = it.appDicDataVOList
//                evaluateTagAdapter.setData(tags)
//            }
//        }
        mineViewModel.evaluateLiveData.observe(this) {
            evaluateAdapter.setData(it)
            if (it.isNullOrEmpty()) {
                mBinding.emptyView.visible()
            } else {
                mBinding.emptyView.gone()
            }
        }
        getEvaluationList()
    }


    private fun getEvaluationList() {
        mineViewModel.getEvaluationList(1, null)
    }


    private fun initTagView() {
        mBinding.rcyTab.apply {
            val layoutFelManager = FlexboxLayoutManager(context)
            layoutFelManager.flexWrap = FlexWrap.WRAP // 按正常方向换行
            layoutFelManager.flexDirection = FlexDirection.ROW // 水平方向
            layoutFelManager.justifyContent = JustifyContent.FLEX_START // 左对齐
            layoutManager = layoutFelManager
            adapter = evaluateTagAdapter
        }
        evaluateTagAdapter.onItemClickListener = { _, position ->
            tags[position].isSelect = !tags[position].isSelect
            evaluateTagAdapter.notifyDataSetChanged()
        }
    }
}